<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0 auto;
            list-style: none;
            text-decoration: none;
        }
        
        .wrapper {
            width: 500px;
            height: 400px;
            position: relative;
        }
        
        ul {
            width: 500px;
            height: 200px;
            overflow: hidden;
        }
        
        li {
            width: 100%;
            height: 100%;
        }
        
        ol {
            width: 100%;
            height: 20px;
            display: flex;
            position: absolute;
            top: 160px;
            left: 150px;
        }
        
        ol li {
            background: #fff;
            width: 20px;
            border-radius: 50px;
            margin: 0 10px;
        }
        
        .active {
            background: red;
        }
        
        .prev {
            width: 30px;
            height: 50px;
            background: #000;
            display: inline-block;
            position: absolute;
            top: 70px;
            left: 10px;
        }
        
        .next {
            width: 30px;
            height: 50px;
            background: #000;
            display: inline-block;
            position: absolute;
            top: 70px;
            right: 10px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <ul class="ulli">
            <li style="background:brown"></li>
            <li style="background:blue"></li>
            <li style="background:chartreuse"></li>
            <li style="background:darkgreen"></li>
            <li style="background:darkmagenta"></li>
        </ul>
        <ol class="olli">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <span class="prev"></span>
        <span class="next"></span>
    </div>
    <script>
        let ulli = document.querySelectorAll('ul>li');
        let olli = document.querySelectorAll('ol>li');
        let prev = document.querySelectorAll('.prev');
        let next = document.querySelectorAll('.next');
        let timer = null; //定时器
        console.log(ulli, olli, prev, next);

        class lunbo {
            constructor(ele) {
                Object.assign(this, ele, {
                    index: 0
                })
                this.init()
            }
            init() {
                this.addEvent()
            }
            addEvent() {
                //左右
                this.prev[0].onclick = () => {
                    this.index--;
                    for (let i = 0; i < this.ulli.length; i++) {
                        this.ulli[i].style.display = 'none';
                        this.olli[i].className = '';
                    }
                    if (this.index < 0) {
                        this.index = 4
                    }
                    this.olli[this.index].className = 'active';
                    this.ulli[this.index].style.display = 'block';
                }
                this.next[0].onclick = () => {
                    this.index++;
                    for (let i = 0; i < this.ulli.length; i++) {
                        this.ulli[i].style.display = 'none';
                        this.olli[i].className = '';
                    }
                    if (this.index >= 5) {
                        this.index = 0
                    }
                    this.olli[this.index].className = 'active';
                    this.ulli[this.index].style.display = 'block';
                }

                for (let i = 0; i < this.olli.length; i++) {
                    this.olli[i].onclick = () => {
                        for (let i = 0; i < this.ulli.length; i++) {
                            this.ulli[i].style.display = 'none';
                            this.olli[i].className = '';
                        }
                        this.index = i;
                        this.olli[i].className = 'active';
                        this.ulli[i].style.display = 'block';
                    }
                }
                setInterval(() => {
                    this.index++;
                    for (let i = 0; i < this.ulli.length; i++) {
                        this.ulli[i].style.display = 'none';
                        this.olli[i].className = '';
                    }
                    if (this.index >= 5) {
                        this.index = 0
                    }
                    this.olli[this.index].className = 'active';
                    this.ulli[this.index].style.display = 'block';
                }, 1000)
            }
        }
        new lunbo({
            ulli,
            olli,
            prev,
            next
        });
    </script>
</body>

</html>